package com.example.testmarkwon

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.example.testmarkwon.AstPrinter.printAst
import com.example.testmarkwon.databinding.ActivityMainBinding
import kotlinx.coroutines.launch

private const val TAG = "AST"

class MainActivity : AppCompatActivity() {

    private var _bd: ActivityMainBinding? = null
    private val bd get() = _bd!!
    private val vm by viewModels<MainActivityViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        initView()
        bindData()
    }

    private fun initView() {
        enableEdgeToEdge()
        _bd = ActivityMainBinding.inflate(LayoutInflater.from(this))
        setContentView(bd.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        bd.tvInput.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(
                s: CharSequence?,
                start: Int,
                count: Int,
                after: Int
            ) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                val text = bd.tvInput.text.toString()
                vm.updateText(text)
            }

            override fun afterTextChanged(s: Editable?) {

            }
        })
        bd.btnPrev.setOnClickListener {
            vm.goPrev()
        }
        bd.btnNext.setOnClickListener {
            vm.goNext()
        }
        bd.btnStart.setOnClickListener {
            vm.goStart()
        }
        bd.btnEnd.setOnClickListener {
            vm.goEnd()
        }
        bd.tvInput.setText("""
            |  表头   | 表头  |
            |  ----  | ----  |
            | 单元格  | 单元格 |
        """.trimIndent())
    }

    private fun bindData() {
        lifecycleScope.launch {
            vm.text.collect { text ->
                Log.d(TAG, "source:\n${text}")
                val node = (App.app.markdown).parse(text)
                val sb = StringBuilder()
                printAst(node, sb)
                val renderResult = App.app.markdown.render(node)
                bd.tvResult.setText(renderResult)
                bd.tvSource.setText(text)
                bd.tvTree.text = sb.toString()
            }
        }
        lifecycleScope.launch {
            vm.currentFrame.collect {
                bd.tvFrameCount.text = it
            }
        }
    }
}